using System;
using System.Collections;
using System.Xml;

namespace UNLV.IAP.WebControls
{
	/// <summary>
	/// Strongly-typed collection of SqlWhereBuilderValue objects.
	/// </summary>
	[Serializable]
	public class SqlWhereBuilderValueCollection : CollectionBase
	{

		public SqlWhereBuilderValue this[ int index ]  
		{
			get  
			{
				return( (SqlWhereBuilderValue) List[index] );
			}
			set  
			{
				List[index] = value;
			}
		}

		public int Add( SqlWhereBuilderValue value )  
		{
			return( List.Add( value ) );
		}

		public int IndexOf( SqlWhereBuilderValue value )  
		{
			return( List.IndexOf( value ) );
		}

		public void Insert( int index, SqlWhereBuilderValue value )  
		{
			List.Insert( index, value );
		}

		public void Remove( SqlWhereBuilderValue value )  
		{
			List.Remove( value );
		}

		public bool Contains( SqlWhereBuilderValue value )  
		{
			// If value is not of type SqlWhereBuilderValue, this will return false.
			return( List.Contains( value ) );
		}

		protected override void OnInsert( int index, Object value )  
		{
			if ( value.GetType() != Type.GetType("UNLV.IAP.WebControls.SqlWhereBuilderValue") )
				throw new ArgumentException( "value must be of type SqlWhereBuilderValue.", "value" );
		}

		protected override void OnRemove( int index, Object value )  
		{
			if ( value.GetType() != Type.GetType("UNLV.IAP.WebControls.SqlWhereBuilderValue") )
				throw new ArgumentException( "value must be of type SqlWhereBuilderValue.", "value" );
		}

		protected override void OnSet( int index, Object oldValue, Object newValue )  
		{
			if ( newValue.GetType() != Type.GetType("UNLV.IAP.WebControls.SqlWhereBuilderValue") )
				throw new ArgumentException( "newValue must be of type SqlWhereBuilderValue.", "newValue" );
		}

		protected override void OnValidate( Object value )  
		{
			if ( value.GetType() != Type.GetType("UNLV.IAP.WebControls.SqlWhereBuilderValue") )
				throw new ArgumentException( "value must be of type SqlWhereBuilderValue." );
		}

		public void DeserializeFromNode(XmlNode n)
		{
			// clear any existing nodes first
			this.Clear();
			
			// select all <value> child nodes
			XmlNodeList nodes = n.SelectNodes("value");

			// loop through the list
			foreach (XmlNode nV in nodes)
			{
				this.Add(new SqlWhereBuilderValue(nV));
			}

		}


		public override bool Equals(object obj)
		{
			bool retValue = true;

			SqlWhereBuilderValueCollection v = obj as SqlWhereBuilderValueCollection;
			if (!(object.ReferenceEquals(v, null)))
			{
				if (v.Count != this.Count)
					retValue = false;
				else
				{
					IEnumerator vEnum = v.GetEnumerator();
					IEnumerator tEnum = this.GetEnumerator();

					while(vEnum.MoveNext() && tEnum.MoveNext())
					{
						if ( !(vEnum.Current.Equals(tEnum.Current)) )
						{
							retValue = false;
							break;
						}
					}
				}
			}
			else
				retValue = false;

			return retValue;
		}

		public override int GetHashCode()
		{
			int hashCode = 0;

			foreach (object o in this)
			{
				hashCode ^= o.GetHashCode();
			}

			return hashCode;
		}


		public static bool operator ==(SqlWhereBuilderValueCollection v1, SqlWhereBuilderValueCollection v2)
		{
			if ( (object.ReferenceEquals(v1, null))
				&& (object.ReferenceEquals(v2, null)) )
				return true;
			else if ( (object.ReferenceEquals(v1, null)) )
				return false;
			else return v1.Equals(v2);
		}

		public static bool operator !=(SqlWhereBuilderValueCollection v1, SqlWhereBuilderValueCollection v2)
		{
			if ( (object.ReferenceEquals(v1, null))
				&& (object.ReferenceEquals(v2, null)) )
				return false;
			else if ( (object.ReferenceEquals(v1, null)) )
				return true;
			else return !(v1.Equals(v2));
		}



	}
}
